#!/bin/sh

################################################################################
#
#	File:		Unload PACESupport.sh
#
#	Contains:	Shell script run during package installation that unloads the
#				PACESupport kext based on the current operating system. This
#				script also removes the older kext packages, if installed.
#
#	Copyright:	(C) 2006-2009 by PACE Anti-Piracy, all rights reserved.
# 
################################################################################

# Assume no error for now
result=0

# Kill any running InterLok executables installed
ps -axcopid,command | grep "InterLok Helper" | awk '{ system("kill -9 "$1) }'

# Unload the correct kext based on the current OS version
# Use the master kext script to unload the kext, if it's installed		
if [ -f /System/Library/Extensions/PACESupportFamily.kext/Contents/Resources/paceunload ]; then
	/System/Library/Extensions/PACESupportFamily.kext/Contents/Resources/paceunload
	result=$?
else
	# Unload the correct kext based on the current OS version
	VERSION=`uname -r`
	MAJOR_VERSION=${VERSION%%.*}
	if [ $MAJOR_VERSION -lt 8 ]; then
		# Panther or below
		/sbin/kextunload -b com.paceap.kext.pacesupport.panther
		result=$?
		if [ $result != 0 ]; then
			# PACESupport - kextunload error. Trying again with old bundle ID.
			/sbin/kextunload -b com.paceap.kext.PACESupport
			result=$?
			if [ $result != 0 ]; then
				if [ -d /System/Library/Extensions/PACESupportFamily.kext ]; then
					# PACESupport - kextunload error. Trying again with old command line.
					/sbin/kextunload "/System/Library/Extensions/PACESupportFamily.kext/Contents/PlugIns/PACESupportPanther.kext"
					result=$?
				fi
				if [ $result != 0 ]; then
					if [ -d /System/Library/Extensions/PACESupport.kext ]; then
						# PACESupport - kextunload error. Trying again with old command line and old location
						/sbin/kextunload "/System/Library/Extensions/PACESupport.kext"
						result=$?
					fi
				fi
			fi
		fi
	elif [ $MAJOR_VERSION -lt 9 ]; then
		# Tiger
		/sbin/kextunload -b com.paceap.kext.pacesupport.tiger
		result=$?
		if [ $result != 0 ]; then
			# PACESupport - kextunload error. Trying again with old bundle ID.
			/sbin/kextunload -b com.paceap.kext.PACESupport2
			result=$?
		fi
	elif [ $MAJOR_VERSION -lt 10 ]; then
		# Leopard
		/sbin/kextunload -b com.paceap.kext.pacesupport.leopard
		result=$?
	else
		# Snow Leopard or greater
		/sbin/kextunload -b com.paceap.kext.pacesupport.snowleopard
		result=$?
	fi
fi

# If any InterLok extensions folders or files are locked, unlock them now.
if [ -d /System/Library/Extensions/PACESupportFamily.kext ]; then
	chflags -R nouchg /System/Library/Extensions/PACESupportFamily.kext
fi

# Unlock the launchd files
if [ -f /Library/LaunchDaemons/PACESupport.plist ]; then
	chflags nouchg /Library/LaunchDaemons/PACESupport.plist
fi

# Unlock the PACESupport startup items folder and contents
if [ -d /Library/StartupItems/PACESupport ]; then
	chflags -R nouchg /Library/StartupItems/PACESupport
fi

# Unlock the PACE Anti-Piracy application support folder and contents.
# Also set the correct ownership folder and contents.
# Lastly, make the folder world writable, just in case.
if [ -d /Library/Application\ Support/PACE\ Anti-Piracy ]; then
	chflags -R nouchg /Library/Application\ Support/PACE\ Anti-Piracy
	chown -R root:admin /Library/Application\ Support/PACE\ Anti-Piracy
	chmod 777 /Library/Application\ Support/PACE\ Anti-Piracy
fi

# If the Languages subfolder exists, then make it world writable too.
# This is necessary because at least one non-PACE created installer doesn't
# set this folder as writable, which in turn can cause UI lockup problems.
if [ -d /Library/Application\ Support/PACE\ Anti-Piracy/Languages ]; then
	chmod 777 /Library/Application\ Support/PACE\ Anti-Piracy/Languages
fi

# Unlock the CFM InterLok Engine
if [ -f /System/Library/CFMSupport/InterLok*Engine ]; then
	chflags nouchg /System/Library/CFMSupport/InterLok*Engine
fi

# Unlock the paceit tool
if [ -f /usr/local/bin/paceit ]; then
	chflags nouchg /usr/local/bin/paceit
fi

# Remove the old kext binaries, if present. This should help prevent older
# installers from successfully loading old kexts.
if [ -d /System/Library/Extensions/PACESupport.kext ]; then
	chflags -R nouchg /System/Library/Extensions/PACESupport.kext
	/bin/rm -rf /System/Library/Extensions/PACESupport.kext/Contents/MacOS/PACESupport
fi

if [ -d /System/Library/Extensions/PACESupport2.kext ]; then
	chflags -R nouchg /System/Library/Extensions/PACESupport2.kext
	/bin/rm -rf /System/Library/Extensions/PACESupport2.kext/Contents/MacOS/PACESupport2
fi

# Delete the older PACESupport start up items, if it exists
if [ -d /System/Library/StartupItems/PACESupport ]; then
	chflags -R nouchg /System/Library/StartupItems/PACESupport
	/bin/rm -rf /System/Library/StartupItems/PACESupport
fi

# Delete the paceit from the old location, if present
if [ -f /usr/bin/paceit ]; then
	chflags nouchg /usr/bin/paceit
	/bin/rm -rf /usr/bin/paceit
fi

# Delete the old InterLok Helper, if present
if [ -f /Library/Application\ Support/PACE\ Anti-Piracy/InterLok\ Helper ]; then
	/bin/rm -rf /Library/Application\ Support/PACE\ Anti-Piracy/InterLok\ Helper
fi

exit 0
